package com.example.win.android_banduyujia.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huchun on 2015/8/12.
 */
public class DBHelper extends SQLiteOpenHelper {
    public static final String  LOG_TAG= DBHelper.class.getSimpleName();

    public  static final String  SQL_COURSE_FORMAT = "DROP TABLE IF  EXISTS %1$s";
    public  static final String  SQL_ORDER_FORMAT = "ORDER TABLE IF  EXISTS %1$s";
    public  static final String  SQL_TODAY_FORMAT = "TODAY TABLE IF  EXISTS %1$s";
    public  static final String  SQL_SENT_FORMAT = "SENT TABLE IF  EXISTS %1$s";
    public  static final String  SQL_NOTIFICATION_FORMAT = "NOTIFICATION TABLE IF  EXISTS %1$s";
    public  static final String  SQL_CREATE_FORMAT = "CREATE TABLE IF NOT  EXISTS %1$s (%2$s);";

    private static final String  DB_NAME="BanDU.db";
    private static final int   DB_VERSION = 1;
    private static List<IDBHelper>   mDBHelper = new ArrayList<IDBHelper>();
    private static DBHelper  mInstance = null;

    public DBHelper(Context context){
         super(context, DB_NAME, null, DB_VERSION);
    }

    public static DBHelper getInstance() throws IllegalAccessException {
        if(mInstance == null) {
            throw new IllegalAccessException("Database helper not init, call init before using.");
        }
        return mInstance;
    }

    public static void init(Context context) {
       if(mInstance == null){
           mInstance = new DBHelper(context);
           addHelpers();
       }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        if(db == null){
            return;
        }
        try{
            db.beginTransaction();
            for(IDBHelper helper: mDBHelper){
                helper.onCreate(db);
            }
            db.setTransactionSuccessful();
        }catch (Exception e){
            e.printStackTrace();

        }finally {
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {

        if(currentVersion == oldVersion){
            return;
        }else if(currentVersion < oldVersion){
             // 从新版本升级到旧版本
            try{
                dropStorage(db);
                onCreate(db);
            }catch (Exception e){

            }finally {

            }
        }
        for(IDBHelper helper: mDBHelper){
            helper.onUpgrade(db, oldVersion, currentVersion);
        }
    }

    @Override
    public synchronized void close() {
        super.close();
        mInstance.close();
        try {
            mInstance.finalize();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    private static void addHelpers() {

    }

    private void dropStorage(SQLiteDatabase db) {
         try{
            db.beginTransaction();
            for(IDBHelper helper : mDBHelper){
                db.execSQL(String.format(SQL_COURSE_FORMAT, helper.getTableName()));
                db.execSQL(String.format(SQL_ORDER_FORMAT, helper.getTableName()));
                db.execSQL(String.format(SQL_TODAY_FORMAT, helper.getTableName()));
                db.execSQL(String.format(SQL_SENT_FORMAT, helper.getTableName()));
                db.execSQL(String.format(SQL_NOTIFICATION_FORMAT, helper.getTableName()));
            }
             db.setTransactionSuccessful();
         }finally {
              db.endTransaction();
         }
    }
}
